package com.ruibang.glass.material.service.impl.bcp;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.constant.MaterialWareHouseCode;
import com.ruibang.glass.material.domain.req.bcp.BcpProductsInReq;
import com.ruibang.glass.material.dto.CpBcpLotDTO;
import com.ruibang.glass.material.entity.MaterialContentLot;
import com.ruibang.glass.material.entity.MaterialLotItem;
import com.ruibang.glass.material.entity.bcp.MaterialBcpProductsIn;
import com.ruibang.glass.material.mapper.bcp.MaterialBcpProductsInMapper;
import com.ruibang.glass.material.service.MaterialContentLotService;
import com.ruibang.glass.material.service.MaterialLotItemService;
import com.ruibang.glass.material.service.bcp.MaterialBcpProductsInService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 半成品入库 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-02
 */
@Service
public class GMaterialBcpProductsInServiceImpl extends ServiceImpl<MaterialBcpProductsInMapper, MaterialBcpProductsIn> implements MaterialBcpProductsInService {


    @Autowired
    private MaterialContentLotService materialContentLotService;

    @Autowired
    private MaterialLotItemService materialLotItemService;

    /**
     * 列表查询
     *
     * @param bcpProductsInReq
     * @return
     */
    @Override
    public IPage<MaterialContentLot> getPage(BcpProductsInReq bcpProductsInReq) {
        List<CpBcpLotDTO> cpBcpLotDTOS = materialLotItemService.getcontentLotIdAndCount();
        LambdaQueryWrapper<MaterialContentLot> queryWrapper = Wrappers.<MaterialContentLot>lambdaQuery();
        if (StringUtils.isNotBlank(bcpProductsInReq.getBatchNumber())) {
            queryWrapper.like(MaterialContentLot::getBatchNumber, bcpProductsInReq.getBatchNumber());
        }
        if (CollectionUtils.isEmpty(cpBcpLotDTOS)) {
            return null;
        } else {
            List<String> collect = cpBcpLotDTOS.stream().map(CpBcpLotDTO::getId).collect(Collectors.toList());
            queryWrapper.in(MaterialContentLot::getBatchNumber, collect);
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getATypeShelf())) {
            queryWrapper.like(MaterialContentLot::getContentCode, bcpProductsInReq.getATypeShelf());
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getProductLine())) {
            queryWrapper.like(MaterialContentLot::getLineName, bcpProductsInReq.getProductLine());
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getStatus())) {
            queryWrapper.like(MaterialContentLot::getInStatus, bcpProductsInReq.getStatus());
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getLevelRg())) {
            queryWrapper.like(MaterialContentLot::getModelLevel, bcpProductsInReq.getLevelRg());
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getLevelFinish())) {
            queryWrapper.like(MaterialContentLot::getLevel, bcpProductsInReq.getLevelFinish());
        }
        if (StringUtils.isNotBlank(bcpProductsInReq.getType())) {
            queryWrapper.like(MaterialContentLot::getContentType, bcpProductsInReq.getType());
        }
        if (bcpProductsInReq.getUpdateStartDate() != null) {
            queryWrapper.ge(MaterialContentLot::getUpdateTime, bcpProductsInReq.getUpdateStartDate());
        }
        if (bcpProductsInReq.getUpdateEndDate() != null) {
            queryWrapper.le(MaterialContentLot::getUpdateTime, bcpProductsInReq.getUpdateEndDate());
        }
        if (bcpProductsInReq.getWarehouseCode().equals(MaterialWareHouseCode.CPK_CK.getMsg())) {
            queryWrapper.eq(MaterialContentLot::getContentType, Constants.TWO_STR);
        }
        if (bcpProductsInReq.getWarehouseCode().equals(MaterialWareHouseCode.BCPK_CK.getMsg())) {
            queryWrapper.in(MaterialContentLot::getContentType, Constants.THREE_STR, Constants.SIX_STR);
        }
        queryWrapper.eq(MaterialContentLot::getInStatus, Constants.SIX_STR);
        Page<MaterialContentLot> materialWarehouseInfoPage = materialContentLotService.page(new Page<>(bcpProductsInReq.getPageNo(), bcpProductsInReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialContentLot::getCreateTime)
        );
        if (materialWarehouseInfoPage.getRecords() != null) {
            List<String> collect = materialWarehouseInfoPage.getRecords().stream().map(MaterialContentLot::getContentCode).collect(Collectors.toList());
            List<MaterialContentLot> materialContentLotList = baseMapper.getMaterialContentLot(collect);
            Map<String, MaterialContentLot> materialContentLotMap = new HashMap<>();
            for (MaterialContentLot materialContentLot : materialContentLotList) {
                materialContentLotMap.put(materialContentLot.getContentCode(), materialContentLot);
            }
            for (MaterialContentLot materialContentLot : materialWarehouseInfoPage.getRecords()) {
                if (materialContentLotMap != null) {
                    if (materialContentLotMap.get(materialContentLot.getContentCode()) != null) {
                        materialContentLot.setWarehouseName(materialContentLotMap.get(materialContentLot.getContentCode()).getWarehouseName());
                        materialContentLot.setStorageLocationName(materialContentLotMap.get(materialContentLot.getContentCode()).getStorageLocationName());
                    }
                }
                for (CpBcpLotDTO cpBcpLotDTO : cpBcpLotDTOS) {
                    if (cpBcpLotDTO.getId().equals(materialContentLot.getBatchNumber())){
                        materialContentLot.setMomNum(cpBcpLotDTO.getCountNumber());
                    }
                }
            }
        }
        return materialWarehouseInfoPage;
    }

    @Override
    public List<MaterialContentLot> getMaterialContentLotList(BcpProductsInReq bcpProductsInReq) {
        if (CollectionUtils.isEmpty(bcpProductsInReq.getIdList())) {
            return null;
        }
        List<MaterialContentLot> list = materialContentLotService.list(Wrappers.<MaterialContentLot>lambdaQuery().in(MaterialContentLot::getLotId, bcpProductsInReq.getIdList()));
        for (MaterialContentLot materialContentLot : list) {
            Long count = materialLotItemService.count(Wrappers.<MaterialLotItem>lambdaQuery().eq(MaterialLotItem::getContentLotId, materialContentLot.getBatchNumber()).eq(MaterialLotItem::getStatus,Constants.ONE_STR));
            if (count == null) {
                materialContentLot.setMomNum(Constants.ZERO_INT);
            } else {
                materialContentLot.setMomNum(count.intValue());
            }
        }

        return list;
    }

    @Override
    public MaterialContentLot getMaterialContentLotById(String id) {
        MaterialContentLot materialContentLot = materialContentLotService.getById(id);
        List<MaterialLotItem> list = materialLotItemService.list(Wrappers.<MaterialLotItem>lambdaQuery().eq(MaterialLotItem::getContentLotId, id));
        materialContentLot.setMaterialLotItemList(list);
        return materialContentLot;
    }
}
